package com.xyx.controller.user;

import com.github.pagehelper.PageInfo;
import com.xyx.entity.Result;
import com.xyx.pojo.Member;
import com.xyx.pojo.SysUser;
import com.xyx.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService memberService;

    /**
     * 用户列表查询
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/member_list")
    public  String member(String name,Model model ,
        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        List<Member> allMembers = memberService.getAllMembers(name, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allMembers);
       // System.out.println(pageInfo);
        model.addAttribute("allmember",allMembers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",name);
        return "member_list";
    }
    /**
     * 删除用户
     */
    @RequestMapping("delMember")
    @ResponseBody
    public Result deleteMemberById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            int i=memberService.deleteMemberById(id);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择要删除的ID！");
            result.setMsg("请选择要删除的ID");
            result.setSuccess(false);
            return   result;
        }
    }
    /**
     * 批量删除用户
     */
    @RequestMapping("deleteAllMember")
    @ResponseBody
    public Result deleteAllMember(String idms) {
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(idms);
        if(idms!=null && !idms.trim().equals("")){

            String[] strArray = idms.split(",");
            List<String> idmList = Arrays.asList(strArray);

            int i = memberService.deleteAllMember(idmList);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择数据！");
            result.setMsg("请选择数据");
            result.setSuccess(false);
            return   result;
        }
    }


    /**
     * 新增用户
     */
    @RequestMapping("/addMember")
    @ResponseBody
    public Result addMember(Member member){
        System.out.println(member);
        Result<Object> result=new Result<>();
        if(member!=null
                && member.getName()!=null
                && !member.getName().trim().equals("")
        ){
            System.out.println(memberService);
            Member memberByName = memberService.getMemberByName(member.getName());
            if(memberByName!=null){
                result.setMsg("用户已经存在");
                result.setSuccess(false);
                return   result;
            }
            int i = memberService.addMember(member);
            if(i>0){
                System.out.println("新增成功");
                result.setMsg("新增成功");
                result.setSuccess(true);
                return   result;
            }else {
                System.out.println("新增失败");
                result.setMsg("新增失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请输入用户名和密码！");
            result.setMsg("请输入用户名和密码");
            result.setSuccess(false);
            return   result;
        }

    }
    @RequestMapping("/getMemberById")
    @ResponseBody
    public Result getMemberById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            Member member = memberService.getMemberById(id);

            if(member!=null){
                result.setData(member);
                System.out.println("查找成功");
                result.setMsg("查找成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("没有此用户");
                result.setMsg("没有此用户");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择ID！");
            result.setMsg("请选择ID");
            result.setSuccess(false);
            return   result;
        }

    }
    /**
     * 编辑用户
     */
    @RequestMapping("/editMember")
    @ResponseBody
    public Result editMember(Member member){
        System.out.println(member);
        Result<Object> result=new Result<>();
        if(member!=null
                && member.getName()!=null
                && !member.getName().trim().equals("")
                && member.getId()!=null
        ){
            System.out.println(member);
            int i = memberService.editMember(member);
            if(i>0){
                System.out.println("修改成功");
                result.setMsg("修改成功");
                result.setSuccess(true);
                return   result;
            }else {
                System.out.println("修改失败");
                result.setMsg("修改失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请输入用户名和ID！");
            result.setMsg("请输入用户名和ID");
            result.setSuccess(false);
            return   result;
        }

    }




}
